<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Redis的数据类型 | 江流</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <script>
	  var _hmt = _hmt || [];
	  (function() {
	    var hm = document.createElement("script");
		hm.src = "https://hm.baidu.com/hm.js?d53065bd59576478808301d9b305d57a";
	    var s = document.getElementsByTagName("script")[0]; 
	    s.parentNode.insertBefore(hm, s);
	  })();
	  </script>        
	  </script>
    <meta name="description" content="我的知识存储空间，不大不小刚刚好！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.f7444686.css" as="style"><link rel="preload" href="/assets/js/app.371ed010.js" as="script"><link rel="preload" href="/assets/js/3.fe7e7b6d.js" as="script"><link rel="preload" href="/assets/js/1.adb5634c.js" as="script"><link rel="preload" href="/assets/js/89.2c0b397f.js" as="script"><link rel="prefetch" href="/assets/js/10.1f1951c3.js"><link rel="prefetch" href="/assets/js/100.61abc83e.js"><link rel="prefetch" href="/assets/js/101.36b1a9cd.js"><link rel="prefetch" href="/assets/js/102.ab6aaef1.js"><link rel="prefetch" href="/assets/js/103.56b65027.js"><link rel="prefetch" href="/assets/js/104.10a9920d.js"><link rel="prefetch" href="/assets/js/105.2af1fce1.js"><link rel="prefetch" href="/assets/js/106.89d909fa.js"><link rel="prefetch" href="/assets/js/107.8a051ba9.js"><link rel="prefetch" href="/assets/js/108.8d32e2bb.js"><link rel="prefetch" href="/assets/js/109.98a9c733.js"><link rel="prefetch" href="/assets/js/11.4f1a38c2.js"><link rel="prefetch" href="/assets/js/110.cb2819bf.js"><link rel="prefetch" href="/assets/js/111.3763e438.js"><link rel="prefetch" href="/assets/js/112.437502fc.js"><link rel="prefetch" href="/assets/js/113.c84b80e9.js"><link rel="prefetch" href="/assets/js/114.e8265ab7.js"><link rel="prefetch" href="/assets/js/115.5950f39b.js"><link rel="prefetch" href="/assets/js/116.7082589d.js"><link rel="prefetch" href="/assets/js/117.9ab33002.js"><link rel="prefetch" href="/assets/js/118.b03051ef.js"><link rel="prefetch" href="/assets/js/119.31fe4216.js"><link rel="prefetch" href="/assets/js/12.397d3f87.js"><link rel="prefetch" href="/assets/js/120.4d52fd08.js"><link rel="prefetch" href="/assets/js/121.024a68dc.js"><link rel="prefetch" href="/assets/js/122.75cd9e9a.js"><link rel="prefetch" href="/assets/js/123.3060f162.js"><link rel="prefetch" href="/assets/js/124.b65e3f9b.js"><link rel="prefetch" href="/assets/js/125.87d06856.js"><link rel="prefetch" href="/assets/js/126.23d0d6ec.js"><link rel="prefetch" href="/assets/js/127.1ff7063e.js"><link rel="prefetch" href="/assets/js/128.e7a78683.js"><link rel="prefetch" href="/assets/js/129.3a378a54.js"><link rel="prefetch" href="/assets/js/13.d1346059.js"><link rel="prefetch" href="/assets/js/130.3820ccc0.js"><link rel="prefetch" href="/assets/js/14.961feb61.js"><link rel="prefetch" href="/assets/js/15.61355499.js"><link rel="prefetch" href="/assets/js/16.6759c278.js"><link rel="prefetch" href="/assets/js/17.39315298.js"><link rel="prefetch" href="/assets/js/18.9995ddc3.js"><link rel="prefetch" href="/assets/js/19.e7007f1f.js"><link rel="prefetch" href="/assets/js/20.8284a0ff.js"><link rel="prefetch" href="/assets/js/21.42efcf40.js"><link rel="prefetch" href="/assets/js/22.ce83188a.js"><link rel="prefetch" href="/assets/js/23.125b374b.js"><link rel="prefetch" href="/assets/js/24.9a24b2f6.js"><link rel="prefetch" href="/assets/js/25.61d7e926.js"><link rel="prefetch" href="/assets/js/26.4c271fce.js"><link rel="prefetch" href="/assets/js/27.08314644.js"><link rel="prefetch" href="/assets/js/28.44a30857.js"><link rel="prefetch" href="/assets/js/29.4b86327a.js"><link rel="prefetch" href="/assets/js/30.284e1438.js"><link rel="prefetch" href="/assets/js/31.c7fb218e.js"><link rel="prefetch" href="/assets/js/32.9591edea.js"><link rel="prefetch" href="/assets/js/33.52d0ee09.js"><link rel="prefetch" href="/assets/js/34.24dce9bc.js"><link rel="prefetch" href="/assets/js/35.e9e59a19.js"><link rel="prefetch" href="/assets/js/36.470d3b70.js"><link rel="prefetch" href="/assets/js/37.f43d4d57.js"><link rel="prefetch" href="/assets/js/38.bb14e7a9.js"><link rel="prefetch" href="/assets/js/39.4238fcd3.js"><link rel="prefetch" href="/assets/js/4.10111819.js"><link rel="prefetch" href="/assets/js/40.eb5830d8.js"><link rel="prefetch" href="/assets/js/41.f2de0dd7.js"><link rel="prefetch" href="/assets/js/42.ee6bc670.js"><link rel="prefetch" href="/assets/js/43.aeb80fc2.js"><link rel="prefetch" href="/assets/js/44.3dcf73d7.js"><link rel="prefetch" href="/assets/js/45.f60dd2b7.js"><link rel="prefetch" href="/assets/js/46.3ffd9a49.js"><link rel="prefetch" href="/assets/js/47.0629cfd8.js"><link rel="prefetch" href="/assets/js/48.4064d363.js"><link rel="prefetch" href="/assets/js/49.e5cd2868.js"><link rel="prefetch" href="/assets/js/5.bbaa5544.js"><link rel="prefetch" href="/assets/js/50.e1524d44.js"><link rel="prefetch" href="/assets/js/51.ef5d60b4.js"><link rel="prefetch" href="/assets/js/52.9e15bbc1.js"><link rel="prefetch" href="/assets/js/53.9e53a9b3.js"><link rel="prefetch" href="/assets/js/54.bda58726.js"><link rel="prefetch" href="/assets/js/55.cdc29215.js"><link rel="prefetch" href="/assets/js/56.3f0607d3.js"><link rel="prefetch" href="/assets/js/57.b45ba0fb.js"><link rel="prefetch" href="/assets/js/58.12450427.js"><link rel="prefetch" href="/assets/js/59.33096dcc.js"><link rel="prefetch" href="/assets/js/6.8df5fb44.js"><link rel="prefetch" href="/assets/js/60.98645e3a.js"><link rel="prefetch" href="/assets/js/61.413d9151.js"><link rel="prefetch" href="/assets/js/62.cc3b9f5a.js"><link rel="prefetch" href="/assets/js/63.05784f29.js"><link rel="prefetch" href="/assets/js/64.d6d2548e.js"><link rel="prefetch" href="/assets/js/65.dfa3ba68.js"><link rel="prefetch" href="/assets/js/66.bb4f5fde.js"><link rel="prefetch" href="/assets/js/67.2c282a4f.js"><link rel="prefetch" href="/assets/js/68.ce435a87.js"><link rel="prefetch" href="/assets/js/69.e439f6e2.js"><link rel="prefetch" href="/assets/js/7.751d2655.js"><link rel="prefetch" href="/assets/js/70.270cc6b6.js"><link rel="prefetch" href="/assets/js/71.ad1e4a0b.js"><link rel="prefetch" href="/assets/js/72.0adf2b53.js"><link rel="prefetch" href="/assets/js/73.72e019b5.js"><link rel="prefetch" href="/assets/js/74.a4eb5a17.js"><link rel="prefetch" href="/assets/js/75.6412ff6f.js"><link rel="prefetch" href="/assets/js/76.7f462fc1.js"><link rel="prefetch" href="/assets/js/77.16502231.js"><link rel="prefetch" href="/assets/js/78.3082b00c.js"><link rel="prefetch" href="/assets/js/79.4f375500.js"><link rel="prefetch" href="/assets/js/8.6c3fab20.js"><link rel="prefetch" href="/assets/js/80.0f2472d6.js"><link rel="prefetch" href="/assets/js/81.136b6c89.js"><link rel="prefetch" href="/assets/js/82.6f5b7de7.js"><link rel="prefetch" href="/assets/js/83.0eb8579b.js"><link rel="prefetch" href="/assets/js/84.b50a036d.js"><link rel="prefetch" href="/assets/js/85.e7b43c1a.js"><link rel="prefetch" href="/assets/js/86.0cbd7e7b.js"><link rel="prefetch" href="/assets/js/87.647fd8a9.js"><link rel="prefetch" href="/assets/js/88.be268039.js"><link rel="prefetch" href="/assets/js/9.ace3bd0d.js"><link rel="prefetch" href="/assets/js/90.fb93a187.js"><link rel="prefetch" href="/assets/js/91.45160178.js"><link rel="prefetch" href="/assets/js/92.4e47d015.js"><link rel="prefetch" href="/assets/js/93.2f9bae72.js"><link rel="prefetch" href="/assets/js/94.c68d9319.js"><link rel="prefetch" href="/assets/js/95.a6932cad.js"><link rel="prefetch" href="/assets/js/96.dea31892.js"><link rel="prefetch" href="/assets/js/97.e8c73484.js"><link rel="prefetch" href="/assets/js/98.bf38dbf7.js"><link rel="prefetch" href="/assets/js/99.edd450af.js">
    <link rel="stylesheet" href="/assets/css/0.styles.f7444686.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-6f8f7dda><div data-v-6f8f7dda><div class="password-shadow password-wrapper-out" style="display:none;" data-v-f68096de data-v-6f8f7dda data-v-6f8f7dda><h3 class="title" data-v-f68096de>江流</h3> <p class="description" data-v-f68096de>我的知识存储空间，不大不小刚刚好！</p> <label id="box" class="inputBox" data-v-f68096de><input type="password" value="" data-v-f68096de> <span data-v-f68096de>Konck! Knock!</span> <button data-v-f68096de>OK</button></label> <div class="footer" data-v-f68096de><span data-v-f68096de><i class="iconfont reco-theme" data-v-f68096de></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-f68096de>vuePress-theme-reco</a></span> <span data-v-f68096de><i class="iconfont reco-copyright" data-v-f68096de></i> <a data-v-f68096de><span data-v-f68096de>江流</span>
          
        <span data-v-f68096de>2018 - </span>
        2023
      </a></span></div></div> <div class="hide" data-v-6f8f7dda><header class="navbar" data-v-6f8f7dda><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="江流" class="logo"> <span class="site-name">江流</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/复盘/" class="nav-link"><i class="undefined"></i>
  复盘
</a></li><li class="dropdown-item"><!----> <a href="/categories/学习总结/" class="nav-link"><i class="undefined"></i>
  学习总结
</a></li><li class="dropdown-item"><!----> <a href="/categories/踩坑/" class="nav-link"><i class="undefined"></i>
  踩坑
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/算法/" class="nav-link"><i class="undefined"></i>
  算法
</a></li><li class="dropdown-item"><!----> <a href="/categories/Java基础/" class="nav-link"><i class="undefined"></i>
  Java基础
</a></li><li class="dropdown-item"><!----> <a href="/categories/高并发编程/" class="nav-link"><i class="undefined"></i>
  高并发编程
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Java框架/" class="nav-link"><i class="undefined"></i>
  Java框架
</a></li><li class="dropdown-item"><!----> <a href="/categories/学习笔记/" class="nav-link"><i class="undefined"></i>
  学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/框架学习/" class="nav-link"><i class="undefined"></i>
  框架学习
</a></li><li class="dropdown-item"><!----> <a href="/categories/Java项目/" class="nav-link"><i class="undefined"></i>
  Java项目
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-note"></i>
      学习笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/major-notes/java/" class="nav-link"><i class="undefined"></i>
  Java笔记整理
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/juc/" class="nav-link"><i class="undefined"></i>
  JUC学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/more-notes/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM核心知识
</a></li><li class="dropdown-item"><!----> <a href="/docs/more-notes/vue.html" class="nav-link"><i class="undefined"></i>
  Vue入门笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/linux/" class="nav-link"><i class="undefined"></i>
  Linux笔记整理
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/redis/" class="nav-link router-link-active"><i class="undefined"></i>
  Redis学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/springcloud/" class="nav-link"><i class="undefined"></i>
  SpringCloud笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/spring/" class="nav-link"><i class="undefined"></i>
  Spring学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/more-notes/shiro.html" class="nav-link"><i class="undefined"></i>
  Shiro学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/android-notes/" class="nav-link"><i class="undefined"></i>
  Android学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/project-notes/" class="nav-link"><i class="undefined"></i>
  项目笔记
</a></li></ul></div></div><div class="nav-item"><a href="/docs/collections/" class="nav-link"><i class="iconfont reco-collection"></i>
  收藏夹
</a></div><div class="nav-item"><a href="/docs/life-logs/" class="nav-link"><i class="iconfont reco-camera"></i>
  生活日志
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-browse"></i>
      常用网站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://www.cxysite.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  程序员导航
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.nowcoder.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  牛客网
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://leetcode-cn.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  力扣
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-person"></i>
      私藏博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://docker.easydoc.net" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  快速入门docker
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.nowcoder.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  牛客网
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://leetcode-cn.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  力扣
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-gitee"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-6f8f7dda></div> <aside class="sidebar" data-v-6f8f7dda><div class="personal-info-wrapper" data-v-cc06b9e8 data-v-6f8f7dda><img src="/avatar.png" alt="author-avatar" class="personal-img" data-v-cc06b9e8> <h3 class="name" data-v-cc06b9e8>
    江流
  </h3> <div class="num" data-v-cc06b9e8><div data-v-cc06b9e8><h3 data-v-cc06b9e8>104</h3> <h6 data-v-cc06b9e8>Articles</h6></div> <div data-v-cc06b9e8><h3 data-v-cc06b9e8>21</h3> <h6 data-v-cc06b9e8>Tags</h6></div></div> <ul class="social-links" data-v-cc06b9e8></ul> <hr data-v-cc06b9e8></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/复盘/" class="nav-link"><i class="undefined"></i>
  复盘
</a></li><li class="dropdown-item"><!----> <a href="/categories/学习总结/" class="nav-link"><i class="undefined"></i>
  学习总结
</a></li><li class="dropdown-item"><!----> <a href="/categories/踩坑/" class="nav-link"><i class="undefined"></i>
  踩坑
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/算法/" class="nav-link"><i class="undefined"></i>
  算法
</a></li><li class="dropdown-item"><!----> <a href="/categories/Java基础/" class="nav-link"><i class="undefined"></i>
  Java基础
</a></li><li class="dropdown-item"><!----> <a href="/categories/高并发编程/" class="nav-link"><i class="undefined"></i>
  高并发编程
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Java框架/" class="nav-link"><i class="undefined"></i>
  Java框架
</a></li><li class="dropdown-item"><!----> <a href="/categories/学习笔记/" class="nav-link"><i class="undefined"></i>
  学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/框架学习/" class="nav-link"><i class="undefined"></i>
  框架学习
</a></li><li class="dropdown-item"><!----> <a href="/categories/Java项目/" class="nav-link"><i class="undefined"></i>
  Java项目
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-note"></i>
      学习笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/major-notes/java/" class="nav-link"><i class="undefined"></i>
  Java笔记整理
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/juc/" class="nav-link"><i class="undefined"></i>
  JUC学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/more-notes/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM核心知识
</a></li><li class="dropdown-item"><!----> <a href="/docs/more-notes/vue.html" class="nav-link"><i class="undefined"></i>
  Vue入门笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/linux/" class="nav-link"><i class="undefined"></i>
  Linux笔记整理
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/redis/" class="nav-link router-link-active"><i class="undefined"></i>
  Redis学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/springcloud/" class="nav-link"><i class="undefined"></i>
  SpringCloud笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/major-notes/spring/" class="nav-link"><i class="undefined"></i>
  Spring学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/more-notes/shiro.html" class="nav-link"><i class="undefined"></i>
  Shiro学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/android-notes/" class="nav-link"><i class="undefined"></i>
  Android学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/docs/project-notes/" class="nav-link"><i class="undefined"></i>
  项目笔记
</a></li></ul></div></div><div class="nav-item"><a href="/docs/collections/" class="nav-link"><i class="iconfont reco-collection"></i>
  收藏夹
</a></div><div class="nav-item"><a href="/docs/life-logs/" class="nav-link"><i class="iconfont reco-camera"></i>
  生活日志
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-browse"></i>
      常用网站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://www.cxysite.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  程序员导航
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.nowcoder.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  牛客网
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://leetcode-cn.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  力扣
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-person"></i>
      私藏博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://docker.easydoc.net" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  快速入门docker
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.nowcoder.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  牛客网
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://leetcode-cn.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  力扣
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-gitee"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Algorithm</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Java</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Juc</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Linux</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Mysql</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Redis</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/major-notes/redis/01-nosq.html" class="sidebar-link">NoSQL</a></li><li><a href="/docs/major-notes/redis/02-redis-brief.html" class="sidebar-link">Redis入门概述</a></li><li><a href="/docs/major-notes/redis/03-data-type.html" aria-current="page" class="active sidebar-link">Redis的数据类型</a></li><li><a href="/docs/major-notes/redis/04-persistence.html" class="sidebar-link">Redis概述</a></li><li><a href="/docs/major-notes/redis/05-transaction.html" class="sidebar-link">Redis事务</a></li><li><a href="/docs/major-notes/redis/06-publish-subscribe.html" class="sidebar-link">Redis的发布和订阅</a></li><li><a href="/docs/major-notes/redis/07-redis-copy.html" class="sidebar-link">Redis的复制</a></li><li><a href="/docs/major-notes/redis/redis-Install.html" class="sidebar-link">Redis的安装教程</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>SpringBoot</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Springcloud</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-f68096de data-v-6f8f7dda><h3 class="title" data-v-f68096de>Redis的数据类型</h3> <!----> <label id="box" class="inputBox" data-v-f68096de><input type="password" value="" data-v-f68096de> <span data-v-f68096de>Konck! Knock!</span> <button data-v-f68096de>OK</button></label> <div class="footer" data-v-f68096de><span data-v-f68096de><i class="iconfont reco-theme" data-v-f68096de></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-f68096de>vuePress-theme-reco</a></span> <span data-v-f68096de><i class="iconfont reco-copyright" data-v-f68096de></i> <a data-v-f68096de><span data-v-f68096de>江流</span>
          
        <span data-v-f68096de>2018 - </span>
        2023
      </a></span></div></div> <div data-v-6f8f7dda><div data-v-6f8f7dda><main class="page"><section style="display:;"><div class="page-title"><h1 class="title">Redis的数据类型</h1> <div data-v-1e62957f><i class="iconfont reco-account" data-v-1e62957f><span data-v-1e62957f>江流</span></i> <!----> <!----> <i class="tags iconfont reco-tag" data-v-1e62957f><span class="tag-item" data-v-1e62957f>Redis</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="redis-的五大数据类型简介"><a href="#redis-的五大数据类型简介" class="header-anchor">#</a> Redis 的五大数据类型简介</h2> <p><strong>String (字符串)：</strong> String 是redis 最基本的数据类型，一个 key 对应一个 value，一个 value 最多可以是 512M。</p> <p><strong>Hash (哈希)：</strong> Hash 是一个键值对集合，类似于 Java 中的 Map&lt;String,Object&gt;，适合用于存储对象</p> <p><strong>List (列表)：</strong> List 是简单的字符串列表，可以根据插入顺序排序。我们可以添加一个元素到列表的头部（左边）或尾部（右边），其底层实际上是一个链表。</p> <p><strong>Set (集合)：</strong> Set 是 String 类型的无序集合，是通过 HashTable 实现的。</p> <p><strong>Zet (有序集合)：</strong> Zset 和 Set 一样也是 String 类型的集合，其不允许有重复的成员。不同的是，每个元素都会关联一个 Double 类型的分数，Redis 正是通过分数来为集合中的成员进行从小到大的排序。Zset 的成员是唯一的，但分数 (Score) 是可以重复的。</p> <h2 id="redis-常见数据类型操作命令"><a href="#redis-常见数据类型操作命令" class="header-anchor">#</a> Redis 常见数据类型操作命令</h2> <p>详见 <a href="http://redisdoc.com/" target="_blank" rel="noopener noreferrer">Redis 命令参考手册<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="redis-键-key-操作命令"><a href="#redis-键-key-操作命令" class="header-anchor">#</a> Redis 键（Key）操作命令</h3> <ul><li>DEL key ：用于在 key 存在时删除 key</li> <li>DUMP key ：序列化给定的 key，并返回被序列化的值</li> <li>EXISTS key ：检查给定的 key 是否存在</li> <li>EXPIRE key ：为给定 key 设置过期时间</li> <li>EXPIREAT key timestamp ：与 EXPIRE 类似，都用于为 key 设置过期时间，不同在于 EXPIPEAT 命令接收的参数为时间戳</li> <li>PEXPIRE key milliseconds-timestamp ：设置 key 过期的时间戳，以毫秒计</li> <li>KEYS pattern ：查找所有符合给定模式的 key</li> <li>MOVE key db ：将当前数据库的 key 移动到给定的数据库中</li> <li>PERSIST key ：移除 key 的过期时间，key 将持久保持</li> <li>PTTL key ：以毫秒单位返回 key 的剩余时间</li> <li>TTL key ：以秒为单位，返回给定 key 的剩余生存时间，-1表示永不过期，-2 表示已过期</li> <li>RANDOMEY ：从当前数据库中随机返回一个 key</li> <li>RENAME key ：修改 key 的名称</li> <li>RENAMENX key newkey ：仅当 newkey 不存在时，将 key 改为 newkey</li> <li>TYPE key ： 返回所存储的值类型</li></ul> <p><strong>keys / del / exists</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接redis客户端</span>
redis-cli 
<span class="token comment">#测试连通性</span>
<span class="token function">ping</span>
<span class="token comment">#设置 k1</span>
<span class="token builtin class-name">set</span> k1 v1
<span class="token comment">#设置 k2</span>
<span class="token builtin class-name">set</span> k2 v2
<span class="token comment">#设置 k3</span>
<span class="token builtin class-name">set</span> k3 v3
<span class="token comment">#查看当前数据库中所有key</span>
keys *
<span class="token comment">#删除 k1</span>
del k1
<span class="token comment">#查看当前数据库中所有key</span>
keys *
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124164159.png" alt="image-20211124164159314"></p> <p><strong>/ move /expire / ttl / type</strong>  演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#将当前数据库中的k2移动到2号数据库中</span>
move k2 <span class="token number">2</span>
<span class="token comment">#查看当前数据库中所有key</span>
keys *
<span class="token comment">#切换到2号数据库</span>
<span class="token keyword">select</span> <span class="token number">2</span>
<span class="token comment">#查看当前数据库中所有key</span>
keys *
<span class="token comment">#为k4设置过期时间</span>
expire k4 <span class="token number">4</span>
<span class="token comment">#查看k4剩余生产时间</span>
ttl k4
<span class="token comment">#查看k2剩余生存时间</span>
ttl k2
<span class="token comment">#查看k2数据类型</span>
<span class="token builtin class-name">type</span> k2
<span class="token comment">#退出</span>
<span class="token builtin class-name">exit</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124165337.png" alt="image-20211124165337432"></p> <h3 id="redis-字符串-string-操作命令"><a href="#redis-字符串-string-操作命令" class="header-anchor">#</a> Redis 字符串（String）操作命令</h3> <ul><li>SET key value ：设置指定 key 的值</li> <li>GET key ：获取指定 key的值</li> <li>GETRANGE key start end ：获取给定的 key 中字符串的子字符串</li> <li>GETSET key value ： 将给定的 key 的值设为 value，并返回 key 的旧值</li> <li>GETBIT key offset ：对 key 所存储的字符串值，获取指定偏移量上的 bit</li> <li>MGET key1 [key2] ：获取所有 (一个或多个) 给定 key 的值</li> <li>SETBIT key offset value ：对 key 所存储的字符串值，设置或清除指定偏移量上的位</li> <li>SETEX key seconds value ：将值 value关联到 key，并将 key 的过期时间设为 seconds (以秒为单位)</li> <li>SETNX key value ：只有在 key 不存在时设置 key 的值</li> <li>SETRANGE key offset value ：用 value 参数覆写给定 key 所存储的字符串值，从偏移量 offset 开始</li> <li>STRLEN key ：返回 key 所存储的字符串值的长度</li> <li>MSET key value [key value] ：同时设置一个或多个 key - value</li> <li>PSETEX key millseconds value ：与 SETEX 命令相似，都是为给定 key 设置生存时间，不同的是 PSETEX 是以毫秒设置 key 的生存时间</li> <li>INCR key ：将 key 中存储的数字值增 1</li> <li>INCRBY key increment ：将 key 所存储的值加上给定的增量值 (increment)</li> <li>INCRBYFLOAT key increment ：将 key 所存储的值加上给定的浮点增量值 (increment)</li> <li>DECR key ：将 key 中存储的数字值减 1</li> <li>DECRBY key decrement ：key 所存储的值减去给定的减量值</li> <li>APPEND key value ：如果 key 已存在并且是一个字符串，APPEND 命令将 value 追加到 key 原来值得末尾</li></ul> <p><strong>set / get / del / append / strlen</strong>  演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#查看当前数据库所有key</span>
keys *
<span class="token comment">#设置 k1</span>
<span class="token builtin class-name">set</span> k1 v1
<span class="token comment">#获取 k3</span>
get k3
<span class="token comment">#删除 k3</span>
del k3
<span class="token comment">#查看当前数据库所有key</span>
keys *
<span class="token comment">#将&quot;_k1&quot;追加到k1末尾</span>
append k1 _k1
<span class="token comment">#查看k1值的长度</span>
strlen k1
<span class="token comment">#查看当前数据库所有key</span>
keys *
<span class="token comment">#查看k1的值</span>
get k1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124173011.png" alt="image-20211124173010573"></p> <p><strong>incr / decr / incrby / decrby</strong> 演示，一定要是数字才能进行加减</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#选择1号数据库</span>
<span class="token keyword">select</span> <span class="token number">1</span>
<span class="token comment">#查看当前数据库中所有值</span>
keys *
<span class="token comment">#设置 k1</span>
<span class="token builtin class-name">set</span> k1 <span class="token number">1</span>
<span class="token comment">#使k1增1</span>
incr k1
<span class="token comment">#查看k1的值</span>
get k1
<span class="token comment">#使k1减1</span>
decr k1
<span class="token comment">#查看k1的值</span>
get k1
<span class="token comment">#使k1加10</span>
incrby k1 <span class="token number">10</span>
<span class="token comment">#查看k1的值</span>
get k1
<span class="token comment">#使k1减10</span>
decrby k1 <span class="token number">10</span>
<span class="token comment">#查看k1的值</span>
get k1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124174000.png" alt="image-20211124174000152"></p> <p><strong>getrange / setrange</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#查看当前数据库所有值</span>
keys *
<span class="token comment">#查看k1的值</span>
get k1
<span class="token comment">#截取k1下标0~2的子字符串</span>
getrange k1 <span class="token number">0</span> <span class="token number">2</span>
<span class="token comment">#用&quot;hello_redis&quot;覆盖k1下标2之后的字符</span>
setrange k1 <span class="token number">2</span> hello_redis
<span class="token comment">#查看k1的值</span>
get k1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124175055.png" alt="image-20211124175055221"></p> <p><strong>setex / setnx</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#查看当前数据库所有值</span>
keys *
<span class="token comment">#设置 k1 生存时间为10s</span>
setex k1 <span class="token number">10</span> hello
<span class="token comment">#查看k1剩余生存时间</span>
ttl k1
<span class="token comment">#查看k1剩余生存时间</span>
ttl k1
<span class="token comment">#查看k1剩余生存时间</span>
ttl k1
<span class="token comment">#获取k1的值</span>
get k1
<span class="token comment">#如果k1存在设置K1的值为hello</span>
setnx k1 hello
<span class="token comment">#如果k1存在设置K1的值为hello_redis</span>
setnx k1 hello_redis
<span class="token comment">#获取k1的值</span>
get k1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124195029.png" alt="image-20211124195028800"></p> <p><strong>mset / mget / msetnx</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#切换到2号数据库</span>
<span class="token keyword">select</span> <span class="token number">2</span>
<span class="token comment">#查看当前数据库中所有值</span>
keys *
<span class="token comment">#批量设置k1、k2、k3</span>
mset k1 v1 k2 v2 k3 v3
<span class="token comment">#查看k1的值</span>
get k1
<span class="token comment">#查看k2的值</span>
get k2
<span class="token comment">#查看k3的值</span>
get k3
<span class="token comment">#批量获取k1、k2、k3的值</span>
mget k1 k2 k3
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124195817.png" alt="image-20211124195817559"></p> <p><strong>getset</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#查看当前数据库中所有值</span>
keys *
<span class="token comment">#获取k1的值</span>
get k1
<span class="token comment">#获取k1的值并将其值设置为hello_redis</span>
getset k1 hello_redis
<span class="token comment">#获取k1的值</span>
get k1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124200337.png" alt="image-20211124200336971"></p> <h3 id="redis-列表-list-操作命令"><a href="#redis-列表-list-操作命令" class="header-anchor">#</a> Redis 列表（List）操作命令</h3> <ul><li>BLPOP key1 [key2] timeout ：移除并获取第一个元素，如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止</li> <li>BRPOP key1 [key2] timeout ：移除并获取列表最后一个元素，如果列表没有元素会阻塞列表直到等待超时或可发现可弹出元素为止</li> <li>BRPOPLPUSH source destination timeout ：从列表中弹出一个值，将弹出的元素插入到另一个列表中并返回它，如果列表没有元素会阻塞列表直到等待超时或可发现可弹出元素为止</li> <li>LINDEX key index ：通过索引获取列表中的元素</li> <li>LINSERT key BEFOREIAFTER pivot value ：在列表的元素前或者后插入元素</li> <li>LLEN key ：获取列表长度</li> <li>LPOP key ：获取列表长度</li> <li>LPUSH key value ：移除并获取列表中第一个元素</li> <li>LPUSHX key value ：将一个或多个值插入到已存在的列表头部</li> <li>LPUSHX key start stop ：获取列表指定范围内的元素</li> <li>LREM key count value ：移除列表元素</li> <li>LSET key index value ：通过索引来设置元素列表</li> <li>LTRIM key start stop ：对一个列表进行修剪，让列表只保留指定区间内的元素，不在指定区间之内的元素都将被删除</li> <li>RPOP key ：移除并获取列表最后一个元素</li> <li>RPOPLPISH source destination ：移除列表中最后一个元素，并将该元素添加到另一个列表并返回</li> <li>RPISH key value1 [value2] ：在列表找那个添加一个或多个值</li> <li>RPUSHX key value ：为已存在的列表添加值</li></ul> <p><strong>lpush / rpush / lrange</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#清空数据库</span>
flushdb
<span class="token comment">#在l1头部插入e1、e2、e3</span>
lpush l1 e1 e2 e3
<span class="token comment">#查看l1的所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token number">3</span>
<span class="token comment">#在l1尾部插入e4、e5、e6</span>
rpush l1 e4 e5 e6
<span class="token comment">#查看l1的所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token number">6</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125103920.png" alt="image-20211125103920426"></p> <p><strong>lpop / rpop</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看l1的所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token number">6</span>
<span class="token comment">#从l1头部弹出一个元素</span>
lpop l1 <span class="token number">1</span>
<span class="token comment">#从l1尾部弹出两个元素</span>
rpop l1 <span class="token number">2</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125104425.png" alt="image-20211125104425605"></p> <p><strong>lindex / llen / lrem</strong>  演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看l1的所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token comment">#获取l1的元素个数</span>
llen l1
<span class="token comment">#从l1头部移除2个e3</span>
lrem l1 <span class="token number">2</span> e3
<span class="token comment">#查看l1的所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125105834.png" alt="image-20211125105834391"></p> <p><strong>rpoplpop / ltrim / lset</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看l1中所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token comment">#移除l1中最后一个元素并将其添加到l2中</span>
rpoplpush l1 l2
<span class="token comment">#查看l1中所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token comment">#查看l2中所有元素</span>
lrange l2 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token comment">#截取l1索引范围为0到2的元素然后赋值给l1</span>
ltrim l1 <span class="token number">0</span> <span class="token number">2</span>
<span class="token comment">#查看l1中所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token comment">#从l1头部开始查找索引值为2的元素并设置其值为e0</span>
lset l1 <span class="token number">2</span> e0
<span class="token comment">#查看l1中所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125110508.png" alt="image-20211125110508053"></p> <p><img src="http://images.xbnote.top/images/20211125111340.png" alt="image-20211125111340482"></p> <p><strong>linsert key before / after value1 value2</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看l1中所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token comment">#在l1中e2前插入元素e3</span>
linsert l1 before e2 e3
<span class="token comment">#在l1中e0后插入元素e-1</span>
linsert l1 after e0 e-1
<span class="token comment">#查看l1中所有元素</span>
lrange l1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125112729.png" alt="image-20211125112709070"></p> <blockquote><p><strong>总结：</strong></p> <ul><li>list 是一个字符串链表，left、right 都可以添加。如果键不存在，则创建新链表；如果键以存在，则新增内容；如果值全部被移除，则对应的键也会消失。</li> <li>链表的操作无论是头还是尾操作，效率都极高，但是如果是对中间元素操作，效率就很惨淡了。</li></ul></blockquote> <h3 id="集合-set-常用操作"><a href="#集合-set-常用操作" class="header-anchor">#</a> 集合（Set）常用操作</h3> <ul><li><p>SADD key member1 [member2] ：向集合中添加一个或多个成员</p></li> <li><p>SCARD key ：获取集合中的成员</p></li> <li><p>SDIFF key [key2] ：返回给定所有集合的差集</p></li> <li><p>SDIFFSTORE destination key1 [key2] ：返回给定所有集合的差集并存储在 destination 中</p></li> <li><p>SINTER key1 [key2] ：返回所有集合的交集</p></li> <li><p>SINTERSTORE destination key1 [key2] ：返回给定所有集合的交集并存储在 destination 中</p></li> <li><p>SISMEMNER key number ：判断 member 元素是否是集合 key 的成员</p></li> <li><p>SMEMBERS key ：返回集合中所有成员</p></li> <li><p>SMOVE source destinastion ：将 member 元素是否是集合 key 的成员</p></li> <li><p>SPOP key ：移除并返回集合中一个或多个随机数</p></li> <li><p>SRANDMEMBER key [count] ：返回一个集合中一个或多个随机数</p></li> <li><p>SREM key member1 [member2] ：移除集合中一个或多个成员</p></li> <li><p>SUNION key1 [key2] ：返回所有给定集合的并集</p></li> <li><p>SUNIONSTORE destination key1 [key2] ：所有给定集合的并集存储在 destination 集合中</p></li> <li><p>SSCAN key cursor [MATCH patterm] [COUNT count] ：迭代集合中的元素</p></li></ul> <p><strong>sadd / smembers / sismember</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#查看当前数据库存在数据数量</span>
dbsize
<span class="token comment">#向集合s1中添加元素v1</span>
sadd s1 v1
<span class="token comment">#向集合s1中添加相同元素v1</span>
sadd s1 v1
<span class="token comment">#向集合s1中添加元素v2、v3、v4、v5</span>
sadd s1 v2 v3 v4 v5
<span class="token comment">#查看集合s1中的元素</span>
smembers s1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124205741.png" alt="image-20211124205741555"></p> <p><strong>scard / srem / srandmember</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#获取集合里面的元素个数</span>
scard s1
<span class="token comment">#删除集合s1中的元素v1</span>
srem s1 v1
<span class="token comment">#从s1中随机取出2个元素</span>
srandmember s1 <span class="token number">2</span>
<span class="token comment">#从s1中随机取出2个元素</span>
srandmember s1 <span class="token number">2</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124211106.png" alt="image-20211124211106669"></p> <p><strong>spop / stop / smove</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看集合s1中的元素</span>
smembers s1
<span class="token comment">#随机出栈1个元素</span>
spop s1 <span class="token number">1</span>
<span class="token comment">#随机出栈1个元素</span>
spop s1 <span class="token number">1</span>
<span class="token comment">#将s1中的v1赋给s1</span>
smove s1 s2 v1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124212414.png" alt="image-20211124212414082"></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#清空数据库</span>
flushdb
<span class="token comment">#查看当前数据库所有数据</span>
keys *
<span class="token comment">#向集合s1添加元素1、2、3、4、5</span>
sadd s1 <span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span>
<span class="token comment">#向集合s2添加元素1、2、a、b、c</span>
sadd s2 <span class="token number">1</span> <span class="token number">2</span> a b c
<span class="token comment">#获取集合s1与s2的差集</span>
<span class="token function">sdiff</span> s1 s2
<span class="token comment">#获取集合s2与s1的差集</span>
<span class="token function">sdiff</span> s2 s1
<span class="token comment">#获取集合s1与s2的交集</span>
sinter s1 s2
<span class="token comment">#获取集合s1与s2的并集</span>
sunion s1 s2
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="http://images.xbnote.top/images/20211124213235.png" alt="image-20211124213234651"></p> <p><img src="http://images.xbnote.top/images/20211124213300.png" alt="image-20211124213300211"></p> <h3 id="redis-哈希-hash-操作命令"><a href="#redis-哈希-hash-操作命令" class="header-anchor">#</a> Redis 哈希（Hash）操作命令</h3> <ul><li><p>HDEL key field1 [field2] ： 删除一个或多个哈希表字段</p></li> <li><p>HEXISTS key field ：查看哈希表 key 中，指定的字段是否存在</p></li> <li><p>HGET key field ：获取存储在哈希表中指定字段的值</p></li> <li><p>HGETALL key ：获取在哈希表中指定 key 的所有字段和值</p></li> <li><p>HINCRBY key field increment ： 为哈希表 key 中的指定字段的整数值加上增量 increment 。</p></li> <li><p>HINCRBYFLOAT key field increment ： 为哈希表 key 中的指定字段的浮点数值加上增量 increment</p></li> <li><p>HKEYS key ： 获取所有哈希表中的字段</p></li> <li><p>HLEN key ：获取哈希表中字段的数量</p></li> <li><p>HMGET key field1 [field2] ：获取所有给定字段的值</p></li> <li><p>HMSET key field1 value1 [field2 value2] ： 同时将多个 field-value (域-值)对设置到哈希表 key 中</p></li> <li><p>HSET key field value ：同时将多个 field-value (域-值)对设置到哈希表 key 中</p></li> <li><p>HSETNX  key field value ：只有在字段 field 不存在时，设置哈希表字段的值</p></li> <li><p>HVALS key ： 获取哈希表中所有值</p></li> <li><p>HSCAN key cursor [MATCH pattern] [COUNT count] ：迭代哈希表中的键值对</p></li></ul> <p><strong>hset / hget  / hmset / hmget / hgetall / hdel</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#清空数据库</span>
flushdb
<span class="token comment">#为h1添加一个哈希键值对k1-v1</span>
hset h1 k1 v1
<span class="token comment">#获取h1中k1的哈希值</span>
hget h1 k1
<span class="token comment">#为h1批量添加键值对k2-v2、k3-v3</span>
hmset h1 k2 v2 k3 v3
<span class="token comment">#批量获取h1中的k1、k2、k3</span>
hmget h1 k1 k2 k3
<span class="token comment">#获取h1中所有字段和值</span>
hgetall h1
<span class="token comment">#删除h1中k1的值</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125115953.png" alt="image-20211125115953513"></p> <p><img src="http://images.xbnote.top/images/20211125120911.png" alt="image-20211125120911724"></p> <p><strong>hlen / hexists  / hsetnx</strong>  演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看h1中所有元素</span>
hgetall h1
<span class="token comment">#获取h1中所有元素个数</span>
hlen h1
<span class="token comment">#查看h1中是否存在k1</span>
hexists h1 k1
<span class="token comment">#为h1中添加键值对k2-v2</span>
hsetnx h1 k2 v2
<span class="token comment">#尝试再次为k2赋值</span>
hsetnx h1 k2 v22
<span class="token comment">#查看h1中所有元素</span>
hgetall h1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125121823.png" alt="image-20211125121823308"></p> <p><strong>hkeys / hvals</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看h1中所有元素</span>
hgetall h1
<span class="token comment">#获取h1中所有的键值</span>
hkeys h1
<span class="token comment">#获取h1中所有键的值</span>
hvals h1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125122432.png" alt="image-20211125122432448"></p> <p><strong>hincrby / hincrbyfloat</strong></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#连接Redis客户端</span>
redis-cli
<span class="token comment">#清空数据库</span>
flushdb
<span class="token comment">#添加h1并添加键值对k1-66</span>
hset h1 k1 <span class="token number">66</span>
<span class="token comment">#为h1中的k1值添加600</span>
hincrby h1 k1 <span class="token number">600</span>
<span class="token comment">#查看h1的键k1的值</span>
hget h1 k1
<span class="token comment">#为h1中的k1值添加0.666</span>
hincrbyfloat h1 k1 <span class="token number">0.666</span>
<span class="token comment">#查看h1的键k1的值</span>
hget h1 k1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125202540.png" alt="image-20211125202540181"></p> <h3 id="redis-有序集合-zset"><a href="#redis-有序集合-zset" class="header-anchor">#</a> Redis 有序集合（Zset）</h3> <ul><li>ZADD key score1 member1 [score2 member2] ： 向有序集合添加一个或多个成员，或者更新已存在成员的分数</li> <li>ZCARD key ：获取有序集合的成员数</li> <li>ZCOUNT key min max ：计算在有序集合中指定区间分数的成员数</li> <li>ZINCRBY key increment member ：有序集合中对指定成员的分数加上增量 increment</li> <li>ZINTERSTORE destination numkeys key [key ...] ：计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中</li> <li>ZLEXCOUNT key min max ：在有序集合中计算指定字典区间内成员数量</li> <li>ZRANGE key start stop [WITHSCORES] ：通过索引区间返回有序集合指定区间内的成员</li> <li>ZRANGEBYLEX key min max [LIMIT offset count] ：通过字典区间返回有序集合的成员</li> <li>ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] ：通过分数返回有序集合指定区间内的成员</li> <li>ZRANK key member ：返回有序集合中指定成员的索引</li> <li>ZREM key member [member ...] ：移除有序集合中的一个或多个成员</li> <li>ZREMRANGEBYLEX key min max ：移除有序集合中给定的字典区间的所有成员</li> <li>ZREMRANGEBYRANK key start stop ：移除有序集合中给定的排名区间的所有成员</li> <li>ZREMRANGEBYSCORE key min max ：移除有序集合中给定的分数区间的所有成员</li> <li>ZREVRANGE key start stop [WITHSCORES] ：返回有序集中指定区间内的成员，通过索引，分数从高到低</li> <li>ZREVRANGEBYSCORE key max min [WITHSCORES] ：返回有序集中指定分数区间内的成员，分数从高到低排序</li> <li>ZREVRANK key member ：返回有序集合中指定成员的排名，有序集成员按分数值递减(从大到小)排序</li> <li>ZSCORE key member ：返回有序集中，成员的分数值</li> <li>ZUNIONSTORE destination numkeys key [key ...] ：计算给定的一个或多个有序集的并集，并存储在新的 key 中</li> <li>ZSCAN key cursor [MATCH pattern] [COUNT count] ：迭代有序集合中的元素（包括元素成员和元素分值）</li></ul> <p><strong>zadd / zrange</strong>  演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#清空数据库</span>
flushdb
<span class="token comment">#添加z1并添加成员v1、v2、v3</span>
zadd z1 <span class="token number">10</span> v1 <span class="token number">20</span> v2 <span class="token number">30</span> v3
<span class="token comment">#通过索引获取z1的所有成员</span>
zrange z1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token comment">#通过索引获取z1的所有成员并按分数从高到低排序</span>
zrange z1 <span class="token number">0</span> <span class="token parameter variable">-1</span> withscores
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125210346.png" alt="image-20211125210345811"></p> <p><strong>zrangebyscore</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>zrange z1 <span class="token number">0</span> <span class="token parameter variable">-1</span> withscores

zrangebyscore z1 <span class="token number">10</span> <span class="token number">30</span>

zrangebyscore z1 <span class="token number">10</span> <span class="token number">30</span> withscores

zrangebyscore z1 <span class="token punctuation">(</span><span class="token number">10</span> <span class="token number">30</span> withscores

zrangebyscore z1 <span class="token number">10</span> <span class="token punctuation">(</span><span class="token number">30</span> withscores

zrangebyscore z1 <span class="token number">10</span> +inf withscores

zrangebyscore z1 <span class="token punctuation">(</span><span class="token number">10</span> +inf withscores

zrangebyscore z1 <span class="token punctuation">(</span><span class="token number">10</span> +inf withscores limit <span class="token number">0</span> <span class="token number">2</span>

zrangebyscore z1 <span class="token punctuation">(</span><span class="token number">10</span> +inf withscores limit <span class="token number">1</span> <span class="token number">3</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125214627.png" alt="image-20211125214627470"></p> <p><img src="http://images.xbnote.top/images/20211125214846.png" alt="image-20211125214846099"></p> <p><img src="http://images.xbnote.top/images/20211125215040.png" alt="image-20211125215040286"></p> <p><img src="http://images.xbnote.top/images/20211125215135.png" alt="image-20211125215135362"></p> <p><strong>zrem / zrevrank</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看z1所有成员</span>
zrange z1 <span class="token number">0</span> <span class="token parameter variable">-1</span>
<span class="token number">1</span><span class="token punctuation">)</span> <span class="token string">&quot;v1&quot;</span>
<span class="token number">2</span><span class="token punctuation">)</span> <span class="token string">&quot;v2&quot;</span>
<span class="token number">3</span><span class="token punctuation">)</span> <span class="token string">&quot;v3&quot;</span>
<span class="token comment">#逆序查找z1中v1成员的下标</span>
zrevrank z1 v1
<span class="token comment">#逆序查找z1中v3成员的下标</span>
zrevrank z1 v3
<span class="token comment">#删除z1中v1、v2成员</span>
zrem z1 v1 v2
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125212853.png" alt="image-20211125212852769"></p> <p><strong>zcard / zcount / zrank / zscore</strong>  演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#查看z1所有成员</span>
zrange z1 <span class="token number">0</span> <span class="token parameter variable">-1</span> withscores
<span class="token comment">#获取z1中分数区间在10~30的成员</span>
zcount z1 <span class="token number">10</span> <span class="token number">30</span>
<span class="token comment">#获取z1中v1所在下标</span>
zrank z1 v1
<span class="token comment">#获取z1中v1所在下标</span>
zrank z1 v2
<span class="token comment">#获取z1中v1所在下标</span>
zrank z1 v3】
<span class="token comment">#获取z1中v1对应的分数</span>
zscore z1 v1
<span class="token comment">#获取z1中v2对应的分数</span>
zscore z1 v2
<span class="token comment">#获取z1中v3对应的分数</span>
zscore z1 v3
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125214044.png" alt="image-20211125214043562"></p> <p><strong>zrevrange / zrevrangebyscore</strong> 演示</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#通过分数获取z1的所有成员</span>
zrange z1 <span class="token number">0</span> <span class="token parameter variable">-1</span> withscores
<span class="token comment">#获取z1中分数在10~20区间的成员并按分数从高到低排序</span>
zrevrangebyscore z1 <span class="token number">10</span> <span class="token number">20</span> withscores
<span class="token comment">#获取z1中分数在20~10区间的成员并按分数从高到低排序</span>
zrevrangebyscore z1 <span class="token number">20</span> <span class="token number">10</span> withscores
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="http://images.xbnote.top/images/20211125211653.png" alt="image-20211125211652992"></p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">最近更新时间: </span> <span class="time">5/6/2023, 6:41:13 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/docs/major-notes/redis/02-redis-brief.html" class="prev">
          Redis入门概述
        </a></span> <span class="next"><a href="/docs/major-notes/redis/04-persistence.html">
          Redis概述
        </a></span></p></div> <div class="comments-wrapper"><!----></div></main></div> <!----></div> <ul class="sub-sidebar sub-sidebar-wrapper" style="width:12rem;" data-v-6986a797 data-v-6f8f7dda><li class="level-2" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#redis-的五大数据类型简介" class="sidebar-link reco-side-redis-的五大数据类型简介" data-v-6986a797>Redis 的五大数据类型简介</a></li><li class="level-2" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#redis-常见数据类型操作命令" class="sidebar-link reco-side-redis-常见数据类型操作命令" data-v-6986a797>Redis 常见数据类型操作命令</a></li><li class="level-3" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#redis-键-key-操作命令" class="sidebar-link reco-side-redis-键-key-操作命令" data-v-6986a797>Redis 键（Key）操作命令</a></li><li class="level-3" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#redis-字符串-string-操作命令" class="sidebar-link reco-side-redis-字符串-string-操作命令" data-v-6986a797>Redis 字符串（String）操作命令</a></li><li class="level-3" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#redis-列表-list-操作命令" class="sidebar-link reco-side-redis-列表-list-操作命令" data-v-6986a797>Redis 列表（List）操作命令</a></li><li class="level-3" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#集合-set-常用操作" class="sidebar-link reco-side-集合-set-常用操作" data-v-6986a797>集合（Set）常用操作</a></li><li class="level-3" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#redis-哈希-hash-操作命令" class="sidebar-link reco-side-redis-哈希-hash-操作命令" data-v-6986a797>Redis 哈希（Hash）操作命令</a></li><li class="level-3" data-v-6986a797><a href="/docs/major-notes/redis/03-data-type.html#redis-有序集合-zset" class="sidebar-link reco-side-redis-有序集合-zset" data-v-6986a797>Redis 有序集合（Zset）</a></li></ul></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.371ed010.js" defer></script><script src="/assets/js/3.fe7e7b6d.js" defer></script><script src="/assets/js/1.adb5634c.js" defer></script><script src="/assets/js/89.2c0b397f.js" defer></script>
  </body>
</html>
